package com.htudata.core.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

@Repository
public class BaseDao {
  public static final Logger LOG = LoggerFactory.getLogger(BaseDao.class);
  
  @Resource
  public SqlSessionTemplate sqlSessionTemplate;
  
  public void createTable(String mapperInterfaceId, String tableName, String sequenceName) {
    Map<String, String> map = new HashMap<>();
    map.put("tableName", tableName);
    map.put("sequenceName", sequenceName);
    mapperInterfaceId = mapperInterfaceId + ".createTable";
    LOG.info("create table interface id {}, tableName:{}.", mapperInterfaceId, tableName);
    this.sqlSessionTemplate.update(mapperInterfaceId, map);
  }
  
  public void createIndex(String mapperInterfaceId, String tableName, String timeIndex, String timeAndNumIndex) {
    Map<String, String> map = new HashMap<>();
    map.put("tableName", tableName);
    map.put("timeIndex", timeIndex);
    map.put("timeAndNumIndex", timeAndNumIndex);
    mapperInterfaceId = mapperInterfaceId + ".createIndex";
    LOG.info("create index interface id {}, tableName:{}, timeIndex:{}, timeAndNumIndex:{}.", new Object[] { mapperInterfaceId, tableName, timeIndex, timeAndNumIndex });
    this.sqlSessionTemplate.update(mapperInterfaceId, map);
  }
  
  public void truncate(String mapperInterfaceId, String tableName) {
    mapperInterfaceId = mapperInterfaceId + ".truncate";
    LOG.info("truncate table interface id {}, tableName:{}.", mapperInterfaceId, tableName);
    Map<String, String> map = new HashMap<>();
    map.put("tableName", tableName);
    this.sqlSessionTemplate.update(mapperInterfaceId, map);
  }
  
  public boolean batchDelete(String mapperInterfaceId, String tableName, Long time, List<String> stationNumList) {
    if (stationNumList == null || stationNumList.size() == 0 || time == null)
      return false; 
    mapperInterfaceId = mapperInterfaceId + ".batchDelete";
    LOG.info("batch delete interface id {}, tableName:{}.", mapperInterfaceId, tableName);
    Map<String, Object> map = new HashMap<>();
    map.put("tableName", tableName);
    map.put("time", time);
    map.put("listVal", stationNumList);
    return (this.sqlSessionTemplate.update(mapperInterfaceId, map) > 0);
  }
  
  public boolean delete(String mapperInterfaceId, String tableName, String stationNum, Long time) {
    if (time == null || stationNum == null)
      return false; 
    mapperInterfaceId = mapperInterfaceId + ".delete";
    LOG.info("delete interface id {}, tableName:{}.", mapperInterfaceId, tableName);
    Map<String, Object> map = new HashMap<>();
    map.put("stationNum", stationNum);
    map.put("time", time);
    map.put("tableName", tableName);
    return (this.sqlSessionTemplate.delete(mapperInterfaceId, map) > 0);
  }
  
  public boolean checkTableExists(String mapperInterfaceId, String tableName) {
    mapperInterfaceId = mapperInterfaceId + ".checkTableExists";
    LOG.info("check table exists interface id {}, tableName:{}.", mapperInterfaceId, tableName);
    return ((Boolean)this.sqlSessionTemplate.selectOne(mapperInterfaceId, tableName)).booleanValue();
  }
  
  public boolean batchSave(String mapperInterfaceId, String tableName, List<Map<String, String>> listMap) {
    if (listMap == null || listMap.size() == 0)
      return false; 
    mapperInterfaceId = mapperInterfaceId + ".batchSave";
    LOG.info("batch save table interface id {}, tableName:{}.", mapperInterfaceId, tableName);
    Map<String, Object> map = new HashMap<>();
    map.put("tableName", tableName);
    map.put("listVal", listMap);
    return (this.sqlSessionTemplate.update(mapperInterfaceId, map) > 0);
  }
  
  public boolean batchUpdate(String mapperInterfaceId, String tableName, List<Map<String, String>> listMap) {
    if (listMap == null || listMap.size() == 0)
      return false; 
    mapperInterfaceId = mapperInterfaceId + ".batchUpdate";
    LOG.info("batch update table interface id {}, tableName:{}.", mapperInterfaceId, tableName);
    Map<String, Object> map = new HashMap<>();
    map.put("tableName", tableName);
    map.put("listVal", listMap);
    return (this.sqlSessionTemplate.update(mapperInterfaceId, map) > 0);
  }
  
  public boolean batchSaveOtherSource(String mapperInterfaceId, String tableName, List<Map<String, Object>> listMap) {
    if (listMap == null || listMap.size() == 0)
      return false; 
    mapperInterfaceId = mapperInterfaceId + ".batchSaveOtherSource";
    LOG.info("batch save other source table interface id {}, tableName:{}.", mapperInterfaceId, tableName);
    Map<String, Object> map = new HashMap<>();
    map.put("tableName", tableName);
    map.put("listVal", listMap);
    return (this.sqlSessionTemplate.update(mapperInterfaceId, map) > 0);
  }
  
  public SqlSessionTemplate getSqlSessionTemplate() {
    return this.sqlSessionTemplate;
  }
}
